#!/usr/bin/python3
# -*- coding:utf-8 -*-

from urllib.request import urlopen
import re


def famouslist_func(url):
    url_response = urlopen(url)
    url_html = url_response.read()
    url_html = url_html.decode("utf-8")
    quate_list = re.findall('<span class="text" itemprop="text">(.*?)</span>', url_html)
    if 0 == len(quate_list):
        return False
    for i in range(len(quate_list)):
        quate_list[i] = quate_list[i].strip("“.”")

    author_list = re.findall('<small class="author" itemprop="author">(.*?)</small>', url_html)
    if 0 == len(author_list):
        return False
    tags_list = []
    tags_list_temp = re.findall('<div class="tags">(.*?)</div>', url_html, re.RegexFlag.DOTALL)
    if 0 == len(tags_list_temp):
        return False
    for tags in tags_list_temp:
        tag_list = re.findall('<a class="tag" href="(.*?)">(.*?)</a>', tags)
        one_tag_list = []
        for tag in tag_list:
            one_tag_list.append(tag[1])
        one_tag_str = ",".join(one_tag_list)
        tags_list.append(one_tag_str)

    for i in range(len(tags_list)):
        print("\t".join([quate_list[i], author_list[i], tags_list[i]]))
    return True
url = "http://quotes.toscrape.com/"
i=1
while True:
    url="http://quotes.toscrape.com/page/"+ str(i) +"/"
    print("正在爬取:",url)
    return_value = True;
    try:
        return_value = famouslist_func(url)
    except:
        i -= 1
        print("重试:", url)
    else:
        if False == return_value:
            print(url, "已无数据")
            print("全部爬完数据了")
            break
    finally:
        i += 1
